// pages/center/selllog/selllog.js
const wxapi = require("../../../utils/wxapi");
const { API_SNZO_URL, API_SNZO_FILE_URL } = require("../../../utils/wxapi");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listOfData: [],
    snzoImgUrl: API_SNZO_FILE_URL,
    pageParams: { pageNum: 1, pageSize: 10 },
    total: 0,
    showLogistics: false,
    logisticsInfo: {},
    userType: 'sellUser',
    okText: null,
    showOrder: false,
    orderInfo: null,
    isTriggered: false,
    isHandle: false
  },

  /**
   * 卖出艺术品
   */
  worksSellLog() {
    const bodyParams = {
      seller: app.globalData.userInfo.cardNo
    }
    wxapi.worksSellLog(bodyParams, this.data.pageParams).then(res => {
      wx.hideLoading();
      if (res.status === 200) {
        this.setData({
          isTriggered: false
        })
        if (this.data.pageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.seachList;
        this.setData({
          listOfData,
          total: res.data.seachTotal,
          canScroll: true
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }

    })
  },

  /**
   * 滑到最底部加载 获取分页数据
   */
  scrollToLower() {
    if (this.data.listOfData.length === this.data.total) {
      return;
    }
    wx.showLoading({
      title: '正在载入更多',
    })
    this.setData({
      canScroll: false
    })
    this.data.pageParams.pageNum++
    setTimeout(() => {
      this.worksSellLog()
    }, 1000);
  },

  /**
   * 下拉刷新
   */
  refresherToLoad() {
    this.data.pageParams.pageNum = 1;
    this.worksSellLog();
  },

  /**
   * 预览艺术品
   * @param {*} e 
   */
  previewImage(e) {
    const worksItem = e.currentTarget.dataset.worksItem;
    wx.previewMedia({
      sources: [
        {
          url: this.data.snzoImgUrl + worksItem.worksPhotoUrl
        }
      ],
    })
  },

  /**
   * 更新物流信息
   */
  updateLogistics(e) {
    const logisticsInfo = e.detail.logisticsInfo;
    wxapi.updateWorksSellLog(logisticsInfo).then(res => {
      if (res.status === 200) {
        wx.showToast({
          title: '更新物流信息成功',
          success: () => {
            this.worksSellLog();
          }
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg || res.message,
          showCancel: false
        })
      }
      this.cancelPopup();
    })
  },

  /**
   * 显示物流弹窗
   * @param {*} e 
   */
  showLogisticsPop(e) {
    const orderInfo = e.currentTarget.dataset.orderInfo;
    if (!orderInfo.phoneNum) {
      return false;
    }
    this.setData({
      showLogistics: true,
      logisticsInfo: orderInfo,
      actionType: orderInfo.standby3 === 5 ? 'confirm' : 'update',
      okText: orderInfo?.standby3 === 2 ? '确定' : ''
    })
  },

  /**
   * 编辑物流信息
   */
  editLogistics(e) {
    const orderInfo = e.currentTarget.dataset.orderInfo;
    this.setData({
      showLogistics: true,
      logisticsInfo: null,
      okText: orderInfo?.tranStatus === 2 ? '确定' : ''
    })
  },

  /**
   * 显示订单弹窗
   */
  showOrderInfo(e) {
    const orderInfo = e.currentTarget.dataset.orderInfo;
    this.setData({
      showOrder: true,
      orderInfo
    })
  },

  /**
   * 同意/不同意 议价
   * @param {*} e 
   */
  bargainWorks(e) {
    if (this.data.isHandle) {
      return;
    }
    const worksInfo = e.detail.worksInfo;
    const bargainType = e.detail.bargainType;
    wx.showModal({
      title: '提示信息',
      content: `您是否确定${bargainType == 5 ? '同意' : '不同意'}买家的议价`,
      complete: (res) => {
        if (res.confirm) {
          this.data.isHandle = true;
          wx.showLoading({
            title: '处理中...',
          })
          const bodyParams = {
            id: worksInfo.id,
            seller: worksInfo.seller,
            tranStatus: bargainType
          }
          wxapi.yesOrNoDeal(bodyParams).then(res => {
            wx.hideLoading()
            this.data.isHandle = false;
            if (res.status === 200) {
              this.setData({
                showOrder: false
              })
              this.worksSellLog();
            } else {
              wx.showModal({
                title: '提示信息',
                content: res.msg,
                showCancel: false
              })
            }
          })
        }
      }
    })
  },

  /**
   * 取消弹窗
   */
  cancelPopup() {
    this.setData({
      showLogistics: false,
      logisticsInfo: null,
      orderInfo: null,
      showOrder: false,
      okText: '确定'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.worksSellLog()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})